package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.*;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.validator.Var;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class RecommendUserService {
    @Reference
    private RecommendUserApi recommendUserApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private QuestionApi questionApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public RecommendUserVo todayBest() {
        //获取登陆用户的id
        Long loginId = UserHolder.getUserId();
        //查询今日佳人
        RecommendUser recommendUser = recommendUserApi.todayBest(loginId);
        if (null == recommendUser) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(RandomUtils.nextLong(1, 99));
            recommendUser.setScore(RandomUtils.nextDouble(60, 75));
        }
        UserInfo todayBest = userInfoApi.findUserById(recommendUser.getUserId());
        RecommendUserVo recommendUserVo = new RecommendUserVo();
        BeanUtils.copyProperties(todayBest, recommendUserVo);
        recommendUserVo.setTags(StringUtils.split(todayBest.getTags(), ','));
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());
        return recommendUserVo;
    }

    public PageResult<RecommendUserVo> recommendList(RecommendUserQueryParam recommendUserQueryParam) {
        Long loginId = UserHolder.getUserId();
        PageResult pageResult = recommendUserApi.findPage(loginId, recommendUserQueryParam.getPage(), recommendUserQueryParam.getPagesize());
        List<RecommendUser> items = pageResult.getItems();
        List<RecommendUserVo> recommendUserVos = new ArrayList<RecommendUserVo>();
        if (CollectionUtils.isNotEmpty(items)) {
            List<RecommendUser> recommendUsers = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId((long) i);
                recommendUser.setScore(RandomUtils.nextDouble(60, 90));
            }
            pageResult.setPages(1L);
            pageResult.setCounts(10L);
        }
        for (RecommendUser item : items) {
            Long userId = item.getUserId();
            UserInfo userInfo = userInfoApi.findUserById(userId);
            RecommendUserVo recommendUserVo = new RecommendUserVo();
            BeanUtils.copyProperties(userInfo, recommendUserVo);
            recommendUserVo.setFateValue(item.getScore().longValue());
            recommendUserVo.setTags(StringUtils.split(userInfo.getTags(), ','));
            recommendUserVos.add(recommendUserVo);
        }
        pageResult.setItems(recommendUserVos);
        return pageResult;
    }

    public RecommendUserVo personalInfo(String personId) {
        Long longInUserId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findUserById(Long.valueOf(personId));
        RecommendUser recommendUser = recommendUserApi.findUserById(personId, longInUserId);
        RecommendUserVo vo = new RecommendUserVo();
        vo.setTags(StringUtils.split(userInfo.getTags(), ','));
        BeanUtils.copyProperties(userInfo, vo);
        if (null == recommendUser) {
            vo.setFateValue((long) RandomUtils.nextDouble(60, 80));
        } else {
            vo.setFateValue(recommendUser.getScore().longValue());
        }
        return vo;
    }

    public String getStrangerQuestions(Long userId) {
        Question strangerQuestion = questionApi.findQuestion(userId);
        String question = "你喜欢我吗";
        if (null != strangerQuestion) {
            question = strangerQuestion.getTxt();
        }
        return question;
    }

    public void postStrangerQuestions(Map<String, Object> param) {
        Long longInUserId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findUserById(longInUserId);
        String nickname = userInfo.getNickname();
        String questions = getStrangerQuestions(Long.valueOf(param.get("userId").toString()));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", longInUserId);
        map.put("nickname", nickname);
        map.put("strangerQuestion", questions);
        map.put("reply", param.get("reply"));
        String msg = JSON.toJSONString(map);
        System.out.println(msg);
        huanXinTemplate.sendMsg(String.valueOf(param.get("userId")), msg);
    }

    public List<NearUserVo> search(String gender, Long distance) {
        List<UserLocationVo> userLocationVos = recommendUserApi.search(UserHolder.getUserId(), distance);
        List<NearUserVo> nearUserVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userLocationVos)) {
            List<Long> collect = userLocationVos.stream().map(UserLocationVo::getUserId).collect(Collectors.toList());
            List<UserInfo> userInfos = userInfoApi.findByBatchIds(collect);
            nearUserVos = userInfos.stream().filter(userInfo -> userInfo.getGender().equals(gender)).map(userInfo ->
                    new NearUserVo(userInfo.getId(), userInfo.getAvatar(), userInfo.getNickname())
            ).collect(Collectors.toList());

        }
        return nearUserVos;
    }

    public void LoveUser(Long recommendId) {
        recommendUserApi.loveUser(recommendId, UserHolder.getUserId());

    }

    public void unloveUser(Long recommendId) {
        recommendUserApi.unloveUser(recommendId, UserHolder.getUserId());
    }

    public List<LeftAndRightSlideVo> leftAndRightSlide() {
        List<UserInfo> userInfoList = userInfoApi.findAlluser();
        Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));

        List<LeftAndRightSlideVo> leftAndRightSlideVoList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Long radomUserId = RandomUtils.nextLong(1, userInfoList.size());
            if (radomUserId != UserHolder.getUserId()) {
                System.out.println(radomUserId);
                LeftAndRightSlideVo vo = new LeftAndRightSlideVo();
                UserInfo userById = userInfoApi.findUserById(radomUserId);
                BeanUtils.copyProperties(userById, vo);
                vo.setTags(StringUtils.split(userById.getTags(), ','));
                leftAndRightSlideVoList.add(vo);
            } else {
                i--;
            }
        }
        System.out.println(leftAndRightSlideVoList);
        return leftAndRightSlideVoList;
    }
}
